package org.jeecg.modules.middleware.quartz.job;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.wlxypt.entity.XlyReportDTO;
import org.jeecg.modules.wlhy.wlxypt.service.XLYService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static java.lang.Thread.sleep;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class HnXyptUploadJobService implements Job {

    @Autowired
    private XLYService xlyService;
    @Autowired
    private IHyDriverService hyDriverService;
    @Autowired
    private IHyVehicleService hyVehicleService;
    @Autowired
    private IHyShippingNoteBillService hyShippingNoteBillService;
    @Override
    public void execute(org.quartz.JobExecutionContext context) {
        log.info("河南信息平台代开批量上报开始");
        // 业务逻辑
        QueryWrapper<XlyReportDTO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("shipping.shipping_status", "5");
        queryWrapper.eq("shipping.shipping_pay_status", "6");
        queryWrapper.eq("bill.bill_type","1");
        queryWrapper.ne("shipping.daoda_upload_status","1");
        queryWrapper.nested(
                qw -> qw.eq("xlyect.upload_status", "2")
                        .or().eq("xlyect.upload_status","0")
                        .or().isNull("xlyect.upload_status")
        );
        queryWrapper.orderByDesc("shipping.pay_dt");
        Page<XlyReportDTO> page = new Page<XlyReportDTO>(1, 100);

        IPage<XlyReportDTO> pageList = xlyService.getXinluyunEtcList(page,queryWrapper);
        int uploadCount = 0;
        if (pageList.getRecords().size()>0){
            for (XlyReportDTO dto : pageList.getRecords()) {
                HyVehicle vehicle = hyVehicleService.getById(dto.getVehicleId());
                HyDriver driver = hyDriverService.getDriverById(dto.getDriverId());
                List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(new LambdaQueryWrapper<HyShippingNoteBill>().eq(HyShippingNoteBill::getShippingId, dto.getId()));
                if (CollUtil.isEmpty(shippingNoteBills)) {
                    log.error("运单号没有流水信息+{}",dto.getShippingNoteNumber());
                    continue;
                }
                try {
                    Result<?> result = xlyService.upload(driver, vehicle, shippingNoteBills, dto);
                    log.info("河南信用平台代开批量上报，{},{},{}",dto.getShippingNoteNumber(),result.getMessage());
                    if (result.isSuccess()){
                        uploadCount++;
                    }
                    sleep(500);
                } catch (InterruptedException e) {
                    log.error("河南信用平台代开批量上报{} , {}", dto.getShippingNoteNumber(), e.getMessage());
                }
            }
        }
        log.info("河南信息平台代开批量上报完成，共上报成功{}条",uploadCount);
    }
}
